package com.starinside.wp.repository.impl;

import com.starinside.wp.domain.observation.ObservationListItemVO;
import com.starinside.wp.domain.observation.ObservationNewRecord;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.ChildrenHealthBaseInfoVO;
import com.starinside.wp.repository.BaseNativeSqlRepository;
import com.starinside.wp.repository.ChildrenObservationRecordRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * *******************************
 * Created by Tim.
 * form com.matt
 * on 2017/8/21
 * *******************************
 */
@Service
public class ChildrenObservationRecordRepositoryImpl extends BaseNativeSqlRepository implements ChildrenObservationRecordRepository {

    @Override
    public List<ObservationListItemVO> findByChildrenIdAndTimeBetween(String childrenId, String beforeTime, String afterTime){
        String sql = "select rr.id, rr.created,cpc.courseName,rr.teacherId,rr.teacherName,rr.score,cpc.typeName " +
                "from observation_new_record rr " +
                "left join course_plan_classes cpc on cpc.id=rr.coursePlanClassesId " +
                "where rr.childrenId = '%s' and rr.deleted=0 and rr.created<'%s' " +
                (StringUtils.isEmpty(afterTime) ? "" : String.format("and rr.created>'%s' ", afterTime)) +
                "order by rr.created";


        sql =  String.format(sql, childrenId, beforeTime);

        List<ObservationListItemVO> items = sqlObjectList(sql, ObservationListItemVO.class);

        return items;
    }

    @Override
    public Page<ObservationListItemVO> findAllByChildrenIds(String[] childrenIds, PagerQuery pagerQuery){
        String sql = "select rr.id, rr.created,cpc.courseName,rr.teacherId,rr.teacherName,rr.score,cpc.typeName, rr.childrenId, c.name as childrenName " +
                "from observation_new_record rr " +
                "left join course_plan_classes cpc on cpc.id=rr.coursePlanClassesId " +
                "left join children c on rr.childrenId = c.id " +
                "where rr.childrenId in ?1 order by rr.created limit ?2,?3";

        List params = new ArrayList();
        params.add(Arrays.asList(childrenIds));
        params.add(pagerQuery.getPageNum() * pagerQuery.getPageSize());
        params.add(pagerQuery.getPageSize());

        List<ObservationListItemVO> items = sqlObjectList(sql, params, ObservationListItemVO.class);

        String countSql = "select count(*)  from observation_new_record rr  where rr.childrenId in ?1";
        params = new ArrayList();
        params.add(Arrays.asList(childrenIds));
        long total = sqlLongResult(countSql, params);

        PageRequest pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());

        return new PageImpl<>(items, pageRequest, total);
    }

    @Override
    public ObservationListItemVO findByRecordId(String recordId){
        String sql = "select rr.id, rr.created,cpc.courseName,rr.teacherId,rr.score,cpc.typeName " +
                "from observation_new_record rr " +
                "left join course_plan_classes cpc on cpc.id=rr.coursePlanClassesId " +
                "where rr.id = '%s' limit 1";

        sql =  String.format(sql, recordId);

        List<ObservationListItemVO> items = sqlObjectList(sql, ObservationListItemVO.class);

        if(items.size() > 0){
            return items.get(0);
        }

        return null;
    }
}
